package com.kl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.domain.DetailedDTO;
import com.kl.domain.FreightDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Detailed;
import com.kl.mapper.DetailedMapper;
import com.kl.service.impl.DetailedServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/detailed")
@Api(value = "明细表管理接口")
public class DetailedController {
    @Autowired
    private DetailedServiceImpl detailedService;

    @Autowired
    private DetailedMapper detailedMapper;

    @ApiOperation(value = "明细表新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "detailed", value = "实体类", required = true, paramType = "body")
    })
    @PostMapping
    public Result add(@RequestBody Detailed detailed) {
        //FreightDTO dto = new FreightDTO();
        // freightServiceFeign.add(dto);
        //String s = JSONObject.toJSONString(FreightDTO);
        // Freight freight = JSONObject.parseObject(s, Freight.class);
        detailedService.add(detailed);

        return Result.build(ConstantEnum.SUCCESS).setResult(detailed);
    }

    @ApiOperation(value = "明细查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "detailed", value = "运单id", required = true, paramType = "body")
    })
    @GetMapping("/getWaybillId")
    public Result<List<DetailedDTO>> findDetailById(@RequestParam Long waybillId) {
        QueryWrapper<Detailed> wrapper = new QueryWrapper();
        wrapper.like("waybill_id",waybillId);
        List<Detailed> list = detailedService.list(wrapper);
        System.out.println(list.size());
        String s = JSON.toJSONString(list);
        List<DetailedDTO> detailedDTOS = JSONObject.parseArray(s, DetailedDTO.class);

        return Result.build(ConstantEnum.SUCCESS).setResult(detailedDTOS);

    }

}
